<?php

$FamilyId = @$_GET['FamilyId'];
$IndividualId = @$_GET['IndividualId'];
$ReportImage = @$_GET['ReportImage'];

$h = @$_GET['height'];
$w = @$_GET['width'];

$default_width = 100;

if (@$ReportImage != null){
    $pathtoimage = "_content/_reports/".$ReportImage.".gif";
    if (@file_exists($pathtoimage))
    {
        $img = imagecreatefromgif( $pathtoimage);
        Header(  "Content-type: image/gif");
        imagegif($img);
        exit;

    } else {

        $img = imagecreatefromgif( "furniture/design/report_thumb_default.gif");
        Header(  "Content-type: image/gif");
        imagegif($img);
        exit;

    }

exit;
}else {


    if(!is_null($FamilyId) or $FamilyId===0) {
        $sql = 'SELECT FamilyPhoto as PHOTO, FamilyPhotoName as PHOTONAME, FamilyPhotoSize as PHOTOSIZE, FamilyPhotoType as PHOTOTYPE from '.$PACTS_cfg->familytablename.' WHERE FamilyId='.$FamilyId;
    }else
    {
        if(!is_null($IndividualId) or $IndividualId===0) {
            $sql = 'SELECT IndividualPhoto as PHOTO, IndividualPhotoName as PHOTONAME, IndividualPhotoSize as PHOTOSIZE, IndividualPhotoType as PHOTOTYPE from '.$PACTS_cfg->individualtablename.' WHERE IndividualId='.$IndividualId;
        } else{
            exit;
        }
    }

    //$sql = "SELECT IndividualPhoto as PHOTO from Individual WHERE IndividualId=0;";

    //echo $sql;
    $result = mysql_query($sql,$PACTS_cfg->dblink);
    //$row = mysql_fetch_array($result)  ;

    //$result=@mysql_query($query);

    while($d = mysql_fetch_array($result)){
        $data = $d["PHOTO"];
        $type = $d["PHOTOTYPE"];
    }

    $im = imagecreatefromstring ($data);
    $ratio     = imagesx($im) / imagesy($im);

    if(is_null($h) and is_null($w) ) {
        $w = $default_width;
        $h = $w/$ratio;
    } else{
        if(is_null($h)) {
            $h = $w/$ratio;
        }
        if(is_null($w) ) {
            $w = $h * $ratio ;
        }
    }


    //$im2 = @imagecreate ($w, $h);
    $im2= imagecreatetruecolor($w, $h);
    $result = imagecopyresampled(
        $im2, $im,
        0, 0, 0, 0,
        $w, $h,
        imagesx($im), imagesy($im)); // resize the image

    imagecopyresized ( $im2, $im, 0, 0, 0, 0, $w, $h, imagesx($im), imagesy($im));


    //	Header(  "Content-type: $type");
    //    echo $im;

    Header(  "Content-type: $type");
    imagejpeg ($im2);
    exit;
}
?>